package io.gitee.wiqer.simple;

import io.gitee.wiqer.ListNode;
import org.junit.Test;

import java.util.concurrent.ThreadLocalRandom;

public class SolutionNC3_EntryNodeOfLoop {
    public ListNode EntryNodeOfLoop(ListNode pHead) {
        ListNode current = pHead;
        while (current != null) { // 只需判断fast即可
            if (current.pass) {
                return current;
            }
            current.pass=true;
            current=current.next;
        }
        return null;
    }
    @Test
    public void test(){
        int sum=5;
        int masterNodeVal= ThreadLocalRandom.current().nextInt()&0x7fffffff%sum;
        int slaveNodeVal= ThreadLocalRandom.current().nextInt()&0x7fffffff%(sum-masterNodeVal)+masterNodeVal;
        System.out.println("masterNodeVal："+masterNodeVal);
        System.out.println("slaveNodeVal:"+slaveNodeVal);
        ListNode masterNode=null;
        ListNode slaveNode=null;
        ListNode next = null;
        ListNode current=null;
        for (int i = 0; i <sum;i++ ) {
            current=new ListNode(i);
            current.next=next;
            next=current;
            if(i==masterNodeVal){
                masterNode=current;
            }
            if(i==slaveNodeVal){
                slaveNode=current;
            }
        }
        masterNode.next=slaveNode;
        System.out.println(EntryNodeOfLoop(current).val);
    }
}
